using Microsoft.AspNetCore.Mvc;
using WebRtcProject.Services;

namespace WebRtcProject.Controllers
{
    /// <summary>
    /// RabbitMQ 外部服务连接管理控制器
    /// 提供队列消息发送和连接状态监控功能
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class RabbitMQController : ControllerBase
    {
        private readonly RabbitMQService _rabbitMQService;
        private readonly ILogger<RabbitMQController> _logger;

        public RabbitMQController(RabbitMQService rabbitMQService, ILogger<RabbitMQController> logger)
        {
            _rabbitMQService = rabbitMQService;
            _logger = logger;
        }

        /// <summary>
        /// 获取RabbitMQ连接状态
        /// </summary>
        [HttpGet("status")]
        public IActionResult GetConnectionStatus()
        {
            try
            {
                var status = _rabbitMQService.GetConnectionStatus();
                return Ok(status);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取RabbitMQ连接状态失败");
                return StatusCode(500, new { error = "获取连接状态失败", message = ex.Message });
            }
        }

        /// <summary>
        /// 发送WebRTC事件消息
        /// </summary>
        [HttpPost("send-webrtc-event")]
        public async Task<IActionResult> SendWebRtcEvent([FromBody] object eventData)
        {
            try
            {
                await _rabbitMQService.PublishWebRtcEventAsync(eventData);
                return Ok(new { message = "WebRTC事件消息发送成功" });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "发送WebRTC事件消息失败");
                return StatusCode(500, new { error = "发送消息失败", message = ex.Message });
            }
        }

        /// <summary>
        /// 发送通知消息
        /// </summary>
        [HttpPost("send-notification")]
        public async Task<IActionResult> SendNotification([FromBody] object notification)
        {
            try
            {
                await _rabbitMQService.PublishNotificationAsync(notification);
                return Ok(new { message = "通知消息发送成功" });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "发送通知消息失败");
                return StatusCode(500, new { error = "发送通知失败", message = ex.Message });
            }
        }

        /// <summary>
        /// 发送自定义消息到指定队列
        /// </summary>
        [HttpPost("send-message")]
        public async Task<IActionResult> SendMessage([FromBody] SendMessageRequest request)
        {
            try
            {
                await _rabbitMQService.PublishMessageAsync(request.QueueName, request.Message, request.RoutingKey);
                return Ok(new { message = $"消息已发送到队列: {request.QueueName}" });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "发送消息到队列 {QueueName} 失败", request.QueueName);
                return StatusCode(500, new { error = "发送消息失败", message = ex.Message });
            }
        }

        /// <summary>
        /// 测试连接
        /// </summary>
        [HttpPost("test-connection")]
        public async Task<IActionResult> TestConnection()
        {
            try
            {
                if (_rabbitMQService.IsConnected)
                {
                    return Ok(new { message = "RabbitMQ连接正常", connected = true });
                }
                else
                {
                    // 尝试重新连接
                    await _rabbitMQService.InitializeAsync();
                    return Ok(new { message = "RabbitMQ连接已重新建立", connected = true });
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "RabbitMQ连接测试失败");
                return StatusCode(500, new { 
                    message = "RabbitMQ连接失败", 
                    connected = false, 
                    error = ex.Message 
                });
            }
        }
    }

    /// <summary>
    /// 发送消息请求模型
    /// </summary>
    public class SendMessageRequest
    {
        public string QueueName { get; set; } = string.Empty;
        public object Message { get; set; } = new object();
        public string? RoutingKey { get; set; }
    }
} 